package com.inhim.pjhim.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.activity.TypeListActivity;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.utils.ImageLoaderUtils;
import com.inhim.pjhim.view.CustomRoundAngleImageView;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import java.io.Serializable;

public class AllCateAdapter extends RecyclerArrayAdapter<ReaderTypeList.List> {

    private Activity context;
    PersonViewHolder viewHolder;
    private String Title,mType;

    public AllCateAdapter(Activity context,String title,String type) {
        super(context);
        this.context=context;
        this.Title=title;
        this.mType=type;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = null;
        if (mView == null) {
            mView = LayoutInflater.from(context).inflate(R.layout.item_all_cate, null);
            viewHolder = new PersonViewHolder(mView);
        } else {
            viewHolder = (PersonViewHolder) mView.getTag();
        }
        return viewHolder;
    }

    public class PersonViewHolder extends BaseViewHolder<ReaderTypeList.List> {
        public CustomRoundAngleImageView circleImageView;
        public TextView textview;
        public ConstraintLayout constran;
        private Handler mMainHandler=new Handler();

        public PersonViewHolder(View itemView) {
            super(itemView);
            circleImageView = getView(R.id.circleImageView);
            textview = getView(R.id.textview);
            constran = getView(R.id.constran);
        }

        @Override
        public void setData(ReaderTypeList.List data) {
            super.setData(data);
            ImageLoaderUtils.setImage(data.getIcon(), circleImageView);
            textview.setText(data.getName());
            if(viewHolder!=null){
                viewHolder.itemView.setOnClickListener(v -> {
                    constran.setBackgroundResource(R.mipmap.background_item_select);
                    Intent intent=new Intent(context, TypeListActivity.class);
                    intent.putExtra("TAG","ReadingTwoFragment");
                    intent.putExtra("ReaderType",data);
                    intent.putExtra("ReaderTypeList", (Serializable) getAllData());
                    intent.putExtra("Title",Title);
                    intent.putExtra("position",getAdapterPosition());
                    intent.putExtra("ReaderTypeId",data.getReaderTypeId());
                    intent.putExtra("Type",mType);
                    context.startActivity(intent);
                    mMainHandler.removeCallbacksAndMessages(null);
                    mMainHandler.postDelayed(() -> constran.setBackgroundColor(Color.WHITE),100);
                });
            }
        }

        /*circleImageView = itemView.findViewById(R.id.circleImageView);
            textview = itemView.findViewById(R.id.textview);
            constran = itemView.findViewById(R.id.constran);
            //动态计算屏幕宽度 一行显示四个item
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) ((context.getResources().getDisplayMetrics().
                    widthPixels - ScreenUtils.dip2px(context, 5)) / 4f),

                    ViewGroup.LayoutParams.MATCH_PARENT);
            constran.setLayoutParams(params);*/

    }

}
